﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using DNSPodForNET;
using System.Configuration;
using FluentScheduler;
namespace RegHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("按C键结束!");

            var reg = new Registry();
            reg.Schedule<RefreshIPJob>().ToRunNow().AndEvery(3).Hours();
            JobManager.Initialize(reg);
            var k = new ConsoleKeyInfo();
            do
            {
                k = Console.ReadKey();
            } while (k.KeyChar.ToString().ToLower() != "c");
            

            //Console.WriteLine(recordid);
        }
       public  static void RefreshIp()
        {
            string domainString =
      ConfigurationManager.AppSettings["domain"];
            var api = new DnsPodDomain();
            dynamic result = api.List(new { keyword = domainString });//此处填写您域名的名称,为确保准确定位,请填写完整的域名,如:baidu.com
            var domains = result.domains;
            //Console.WriteLine(domains.ToString());
            var domain = domains[1];
            var _domainId = Convert.ToInt32(domain.id);
            //Console.WriteLine(_domainId);
            var record = new DnsPodRecord();
            var results = record.List(_domainId).records;


            var req = WebRequest.Create("http://members.3322.org/dyndns/getip") as HttpWebRequest;
            req.Method = "GET";
            string myip = string.Empty;
            using (WebResponse wr = req.GetResponse())
            {
                var s = wr.GetResponseStream();
                myip = new StreamReader(s, Encoding.GetEncoding("gb2312")).ReadToEnd();
                Console.WriteLine("当前IP地址为:" + myip+" 当前时间为:"+DateTime.Now);


                //在这里对接收到的页面内容进行处理
            }
            foreach (var VARIABLE in results)
            {
                int recordid = 0;
                //Console.WriteLine(VARIABLE.name);
                if (VARIABLE.name == "www" || VARIABLE.name=="*" || VARIABLE.name=="@")
                {
                    recordid = VARIABLE.id;
                }                

                if (recordid != 0 && !string.IsNullOrEmpty(myip))
                {
                    if (record.Ddns(_domainId, recordid, "*", myip))
                    {
                        Console.WriteLine("* IP更改成功！");
                    }
                    if (record.Ddns(_domainId, recordid, "@", myip))
                    {
                        Console.WriteLine("@ IP更改成功！");
                    }
                    if (record.Ddns(_domainId, recordid, "www", myip))
                    {
                        Console.WriteLine("WWW IP更改成功！");
                    }

                }
            }
            Console.WriteLine(results);
            Console.WriteLine("更新完毕!");
        }
    }
    
    public class RefreshIPJob : IJob
    {
        public void Execute()
        {
            try
            {
                Program.RefreshIp();
            }
            catch (Exception e)
            {

                Console.WriteLine(e);
            }
        }
    }
}
